using System;
using System.Runtime.CompilerServices;
using UnityEngine;
using Voodoo.Sauce.Internal.Analytics;
using Voodoo.Sauce.Internal.Analytics.Trackers;
using Voodoo.Sauce.Internal.CrossPromo.Canvas;
using Voodoo.Sauce.Internal.CrossPromo.Models;
using Voodoo.Sauce.Internal.CrossPromo.RestClient;
using Voodoo.Sauce.Internal.CrossPromo.Utils;

namespace Voodoo.Sauce.Internal.CrossPromo
{
	internal static class VoodooCrossPromo
	{
		public static readonly string TAG = "CrossPromotion";

		public static EventManager Callback;

		public static CrossPromoInfo Info;

		private static IRestClient _restClient;

		private static bool _isInit;

		private static Action<AssetModel> _onSuccess;

		private static Action _onFailure;

		private static bool _showCalledBeforeInit;

		[CompilerGenerated]
		private static EventManager.InitEventHandler _003C_003Ef__mg_0024cache0;

		[CompilerGenerated]
		private static EventManager.EventHandler _003C_003Ef__mg_0024cache1;

		[CompilerGenerated]
		private static EventManager.EventHandler _003C_003Ef__mg_0024cache2;

		[CompilerGenerated]
		private static EventManager.ErrorHandler _003C_003Ef__mg_0024cache3;

		[CompilerGenerated]
		private static Action _003C_003Ef__mg_0024cache4;

		[CompilerGenerated]
		private static Action _003C_003Ef__mg_0024cache5;

		public static void Init()
		{
			if (VoodooSauce.IsPremium())
			{
				VoodooLog.Log(TAG, "User is PREMIUM, cross promo is disabled.");
			}
			else
			{
				try
				{
					if (_isInit)
					{
						VoodooLog.LogW(TAG, "You already init the cross promotion.");
					}
					else
					{
						CrossPromoInfo crossPromoInfo = new CrossPromoInfo();
						crossPromoInfo.CrossPromoIsReady = false;
						crossPromoInfo.Format = "BigSquare";
						Info = crossPromoInfo;
						RegisterInternalCallbacks();
						_restClient = UnityRestClient.CreateGameObject();
						CacheManager.CreateCacheIfNoExist();
						_restClient.GetGameInfoAndDownload();
					}
				}
				catch (Exception ex)
				{
					VoodooLog.LogW(TAG, ex.ToString());
				}
				finally
				{
					_isInit = true;
					if (_showCalledBeforeInit)
					{
						Show(_onSuccess, _onFailure);
					}
				}
			}
		}

		private static void RegisterInternalCallbacks()
		{
			Callback = new EventManager();
			Callback.initComplete += InvokeInitCompleteCallback_Internal;
			Callback.click += InvokeOnClickCallback_Internal;
			Callback.shown += InvokeOnShownCallback_Internal;
			Callback.error += InvokeOnErrorReceivedCallback_Internal;
		}

		public static void Show(Action<AssetModel> onSuccess, Action onFailure)
		{
			if (!_isInit)
			{
				_showCalledBeforeInit = true;
			}
			try
			{
				_onSuccess = onSuccess;
				_onFailure = onFailure;
				if (!_isInit || VoodooSauce.IsPremium())
				{
					onFailure?.Invoke();
				}
				else
				{
					if (!_restClient.IsRetrieveInfo() && !Info.HasInternet)
					{
						_restClient.GetGameInfoAndDownload();
					}
					if (AFormatManager.Instance == null)
					{
						((UnityRestClient)_restClient).WaitForFirstFrame(CrossPromoEvents.TriggerShow);
					}
					else
					{
						CrossPromoEvents.TriggerShow();
					}
				}
			}
			catch (Exception message)
			{
				Callback.ErrorEvent(message);
			}
		}

		public static void Hide()
		{
			if (!_isInit)
			{
				_showCalledBeforeInit = false;
			}
			try
			{
				if (_isInit)
				{
					if (AFormatManager.Instance == null)
					{
						((UnityRestClient)_restClient).WaitForFirstFrame(CrossPromoEvents.TriggerHide);
					}
					else
					{
						CrossPromoEvents.TriggerHide();
					}
				}
			}
			catch (Exception message)
			{
				Callback.ErrorEvent(message);
			}
		}

		private static void InvokeInitCompleteCallback_Internal(string format)
		{
			Info.CrossPromoIsReady = true;
		}

		private static void InvokeOnShownCallback_Internal(AssetModel asset)
		{
			if (_onSuccess != null)
			{
				_onSuccess(asset);
			}
			if (asset != null)
			{
				if (!string.IsNullOrEmpty(asset.tracking_impression))
				{
					new WWW(asset.tracking_impression);
				}
			//	VoodooAnalytics.OnCrossPromoShown(asset);
				VoodooTrackers.TrackCrossPromoEvent("impression", asset.game.bundle_id, asset.file_path);
				if (_restClient != null)
				{
					_restClient.BufferVideosInCache();
				}
			}
		}

		private static void InvokeOnClickCallback_Internal(AssetModel asset)
		{
			if (asset != null)
			{
			//	VoodooAnalytics.OnCrossPromoClick(asset);
				VoodooTrackers.TrackCrossPromoEvent("click", asset.game.bundle_id, asset.file_path);
			}
		}

		private static void InvokeOnErrorReceivedCallback_Internal(Exception e)
		{
			if (_onFailure != null)
			{
				_onFailure();
			}
			if (e == null)
			{
				VoodooLog.Log(TAG, "No video to display. Check your internet connection,  you already installed all the games or there is no game to promote.");
				return;
			}
			VoodooLog.LogW(TAG, e.Message);
			//VoodooAnalytics.OnCrossPromoError(e.Message);
		}
	}
}
